import utime
import st7789
from machine import SPI, Pin,I2C
from Aht20 import aht20
from YL69 import yl69
from Paj7620u2 import PAJ7620
G05 = Pin(5)
G05.init(Pin.OUT)
spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8))
display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT))
i2c = I2C(0, sda=Pin(13), scl=Pin(14), freq=400000) 
i2cAddress = i2c.scan()
se=PAJ7620(i2c,i2cAddress[0])
se.set_gesture(1,0xff)
se.get_gesture(1)
y=0
x=77
def drive():
    tsdu=yl69()
    if(tsdu<x):
        G05.off()#开
        utime.sleep(1)
        G05.on()
    else:
        G05.on()#关

def star ():
    display.fill(st7789.color565(0,0,255))
    while 1:
        data=aht20()
        wendu=data[1]
        shidu=data[0]
        Tshidu=yl69()
        drive()
        utime.sleep_ms(100)
        display.draw_string(0, 0, 'Temp: %.2f' % (wendu), color=st7789.color565(66, 133, 244), bg=st7789.color565(0, 0, 255), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 60, 'Humi: %.2f' % (shidu), color=st7789.color565(66, 133, 244), bg=st7789.color565(0, 0, 255), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 120, 'SLHumi: %.2f' % (Tshidu), color=st7789.color565(66, 133, 244), bg=st7789.color565(0, 0, 255), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        Gd=se.get_ggesture(1)
        if(Gd=='pr'):
            break
def scend():   
    while 1:
        utime.sleep(0.5)
        display.fill(st7789.color565(0,0,255))
        global y
        display.fill_rect(0, y, 240, 40, st7789.color565(0,255,0))
        display.draw_string(0, 0, 'Sunflower', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 40, 'Rose', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 80, 'Tulip', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 120, 'Lily', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 160, 'Daffodil', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        display.draw_string(0, 200, 'Orchid', color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        Gd=se.get_ggesture(1)
        if(Gd=='up'):
            if(y<40):
                y=y+240
            y=y-40
        elif(Gd=='dw'):
            if(y>200):
                y=y-240
            y=y+40
        elif(Gd=='rt'):
            break
        elif(Gd=='lt'):
            break
        elif(Gd=='Ok'):
            return y
def third():
    display.fill(st7789.color565(0,0,255))
    display.draw_string(5, 10, 'Please adjust', color=st7789.color565(0, 0, 0), bg=st7789.color565(0, 0, 255), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
    display.draw_string(5, 40, 'by yourself', color=st7789.color565(0, 0, 0), bg=st7789.color565(0, 0, 255), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
    while 1:
        utime.sleep_ms(50)
        display.fill_rect(0, 120, 240, 40, st7789.color565(0,0,255))
        global x
        display.draw_string(5, 120, 'SLHumi: %.2f' % (x), color=st7789.color565(66, 133, 244), bg=st7789.color565(66, 133, 244), size=3, vertical=False, rotate=st7789.ROTATE_0, spacing=1)
        Gd=se.get_ggesture(1)
        if(Gd=='up'):
            if(x>=87):
                x=x-22
            x=x+1
        elif(Gd=='dw'):
            if(x<=66):
                x=x+22
            x=x-1
        elif(Gd=='rt'):
            break
        elif(Gd=='lt'):
            break
        elif(Gd=='Ok'):
            return x   
while 1:
    star()
    scend()
    third()


